package com.zretc.support.service.serviceimpl;

import com.zretc.support.dao.LogDao;
import com.zretc.support.entity.Log;
import com.zretc.support.entity.User;
import com.zretc.support.service.LogService;
import com.zretc.support.tool.AjaxResponse;
import com.zretc.support.tool.PageData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;

@Service
public class LogServiceImpl implements LogService {
    @Autowired
    LogDao logDao;
    //返回的类
    AjaxResponse ajaxResponse = new AjaxResponse();
    //
    PageData<Log> pageData = new PageData<>();
    //
    SimpleDateFormat  simple = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    Logger logger = LoggerFactory.getLogger(LogServiceImpl.class);

    @Override
    public AjaxResponse addLog(Log log) {
        //获取时间
        log.setLogDate(new Date());
        //调用save方法添加
        logDao.save(log);

        ajaxResponse.setCode(200);
        ajaxResponse.setMsg("添加成功");
        return ajaxResponse;
    }


    /**
     * 分页+模糊查询
     * @param userName 用户姓名
     * @param page 当前页码
     * @param limit 每页的数量
     * @return
     */
    @Override
    public PageData<Log> findLogByLike(String userName, int page, int limit) {

        Pageable pageable = PageRequest.of(page,limit);

        Page<Log> pagedata = logDao.findByLike(userName,pageable);
        //每页的集合数据
        pageData.setRecords(pagedata.getContent());
        //当前页
        pageData.setCurrent(pagedata.getNumber()+1);
        //总记录数
        pageData.setTotal((int)pagedata.getTotalElements());
        //每页的记录数
        pageData.setSize(pagedata.getSize());
        //
        pageData.setCode(200);
        //
        pageData.setMsg("查询成功");

        return pageData;
    }

    @Override
    public PageData<Log> findLogByDate(String userName, String startDate, String endDate, int page, int limit) {
        Pageable pageable = PageRequest.of(page,limit);

        Page<Log> pagedata = null;
        //时间为空
        if (startDate==""&&endDate==""){
            pagedata = logDao.findByLike(userName,pageable);
        }else{
            //将字符串转化为时间格式
            Date start = null ;
            Date end = null ;
            try {
                start = simple.parse(startDate);
                end = simple.parse(endDate);
            } catch (ParseException e) {
                e.printStackTrace();
            }

            logger.info("start:"+start);
            logger.info("end:"+end);
            logger.info("startdate:"+startDate);
            logger.info("enddate:"+endDate);
            System.out.println(start);
            System.out.println(end);
            //时间不为空
            pagedata = logDao.findByDate(userName,start,end,pageable);
            logger.info("page"+pagedata.getContent());
        }

        //每页的集合数据
        pageData.setRecords(pagedata.getContent());
        //当前页
        pageData.setCurrent(pagedata.getNumber()+1);
        //总记录数
        pageData.setTotal((int)pagedata.getTotalElements());
        //每页的记录数
        pageData.setSize(pagedata.getSize());
        //
        pageData.setCode(200);
        //
        pageData.setMsg("查询成功");

        return pageData;
    }
}
